﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using nRoute.Components;
using nRoute.Mapping;
using nRoute.ViewModels;
using SimpleShell.Views;
using SimpleShell.ViewServices;
using nRoute.Utilities;

namespace SimpleShell.ViewModels
{
    [DefineNavigationContent("Content/PageC", typeof(PageCView))]
    [MapViewModel(typeof(PageCView))]
    public class PageCViewModel : NavigationViewModelBase
    {
        private readonly string[] _options = new string[] { "Good", "Bad", "Ugly" };
        private readonly IMessageViewService _messageViewService;
        private string _selectedOption;

        public PageCViewModel(IMessageViewService messageViewService) 
        {
            Guard.ArgumentNotNull(messageViewService, "messageViewService");
            _messageViewService = messageViewService;

            SetupCommands();
        }

        public IEnumerable<string> Options
        {
            get { return _options; }
        }

        public string SelectedOption
        {
            get { return _selectedOption; }
            set
            {
                if (_selectedOption != value)
                {
                    _selectedOption = value;
                    base.NotifyPropertyChanged(() => SelectedOption);
                }
            }
        }

        public ActionCommand SignalCommand { get; private set; }

        protected override ParametersCollection OnSaveState()
        {
            var _state = new ParametersCollection();
            _state.Add("SelOpt", this.SelectedOption);
            return _state;
        }

        protected override void OnRestoreState(ParametersCollection state)
        {
            if (state != null && state.ContainsKey("SelOpt"))
            {
                this.SelectedOption = state.GetValueOrDefault<string>("SelOpt");
            }
        }

        private void SetupCommands()
        {
            this.SignalCommand = new ActionCommand(() =>
            {
                if (string.IsNullOrWhiteSpace(this.SelectedOption))
                {
                    _messageViewService.ShowMessage("No option selected");
                }
                else
                {
                    _messageViewService.ShowMessage(string.Format("You've selected '{0}' option", this.SelectedOption));
                }
            });
        }
    }
}
